$(function () {
    $("#jqGrid").Grid({
        url: '../sparepartout/list',
        colModel: [
            // {label: '出库仓库ID', name: 'outStoreId', index: 'OUT_STORE_ID', width: 80},
            {label: '出库仓库', name: 'outStore', index: 'OUT_STORE', width: 80},
            {label: '出库单号', name: 'outCode', index: 'OUT_CODE', width: 80},

            {label: '名称', name: 'name', index: 'NAME', width: 80},
            {label: '物料编码', name: 'partCode', width: 80},
            {label: '规格', name: 'spec', index: 'SPEC', width: 80},
            {label: '单位', name: 'partUnit', index: 'PART_UNIT', width: 40},
            // {label: '出库数量', name: 'outNum', index: 'OUT_NUM', width: 50},
            // {label: '退库数量', name: 'backNum', index: 'BACK_NUM', width: 50},
            {label: '出库量', name: 'relNum', index: 'REL_NUM', width: 50},
            {label: '领用单位', name: 'useUnit', index: 'USE_UNIT', width: 80},
            {label: '领用人员', name: 'createUserName', index: 'createUserName', width: 80},
            // {label: '维修单号ID', name: 'repairNoId', index: 'REPAIR_NO_ID', width: 80},
            {label: '维修单号', name: 'repairNo', index: 'REPAIR_NO', width: 80},
            // {label: '领料单号ID', name: 'pickNoId', index: 'PICK_NO_ID', width: 80},
            {label: '领料单号', name: 'pickNo', index: 'PICK_NO', width: 80},
            // {label: '使用设备ID', name: 'useDevId', index: 'USE_DEV_ID', width: 80},
            {label: '使用设备码', name: 'useDevCode', index: 'USE_DEV_CODE', width: 80},
            {label: '使用设备', name: 'useDevName', index: 'USE_DEV_NAME', width: 80},
            {
                label: '出库日期', name: 'outDate', index: 'OUT_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },

            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            // {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            // {label: '货位', name: 'cargoLocation', index: 'CARGO_LOCATION', width: 80},
            // {label: '物料编码', name: 'partCode', index: 'PART_CODE', width: 80},
            // {label: '审核时间', name: 'checkDate', index: 'CHECK_DATE', width: 80},
           /* {
                label: '审核状态', name: 'checkStatus', index: 'CHECK_STATUS', width: 80, formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },*/
            // {label: '审核人', name: 'checkUser', index: 'CHECK_USER', width: 80},
            // {label: '修改时间', name: 'updateDate', index: 'UPDATE_DATE', width: 80},
            // {label: '修改人员', name: 'updateUser', index: 'UPDATE_USER', width: 80},
            // {label: '添加时间', name: 'createDate', index: 'CREATE_DATE', width: 80}
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        sparePartOut: {
            delFlag: '',
            remark: '',
            useDevId: '',
            useDevName: '',
            useDevCode: '',
            pickNoId: '',
            pickNo: '',
            repairNoId: '',
            repairNo: '',
            backNum: '',
            outNum: '',
            cargoLocation: '',
            partUnit: '',
            spec: '',
            name: '',
            partCode: '',
            outStoreId: '',
            outStore: '',
            useUnit: '',
            outDate: '',
            outCode: '',
            id: '',
            checkDate: '',
            checkStatus: '1', //未审核
            checkUser: '',
            updateDate: '',
            updateUser: '',
            createUser: '',
            createDate: '',
        },
        ruleValidate: {
            partCode: [
                {required: true, message: '物料编码不能为空', trigger: 'change'}
            ],
            outNum: [
                {required: true, message: '出库数量不能为空', }
            ],
            useDevCode: [
                {required: true, message: '使用设备码不能为空', trigger: 'change'}
            ]
        },
        q: {
            outCode:'', // 出库编码
            partName: '', // 备件名称
            spec: '', // 备件规格
            producor:'', // 供应厂家
            beginTime: '', // 起始时间
            endTime:'' // 结束
        },
        sparePart: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.sparePartOut = {
                delFlag: 1,
                remark: '',
                useDevId: '',
                useDevName: '',
                useDevCode: '',
                pickNoId: '',
                pickNo: '',
                repairNoId: '',
                repairNo: '',
                backNum: '',
                outNum: '',
                cargoLocation: '',
                partUnit: '',
                spec: '',
                name: '',
                partCode: '',
                outStoreId: '',
                outStore: '',
                useUnit: '',
                outDate: new Date(),
                outCode: '',
                id: '',
                checkDate: '',
                checkStatus: '1', //未审核
                checkUser: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
                createDate: '',
            };
            var total = 0;
            var date = new Date();
            Ajax.request({
                url: "../sparepartout/count",
                async: false,
                successCallback: function (r) {
                    total = r.total + 1;
                //    vm.sparePartOut.outCode = 'x-' + date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + (Array(4).join(0) + total).slice(-4);
                }
            });
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.sparePartOut = {
                delFlag: '',
                remark: '',
                useDevId: '',
                useDevName: '',
                useDevCode: '',
                pickNoId: '',
                pickNo: '',
                repairNoId: '',
                repairNo: '',
                backNum: '',
                outNum: '',
                cargoLocation: '',
                partUnit: '',
                spec: '',
                name: '',
                partCode: '',
                outStoreId: '',
                outStore: '',
                useUnit: '',
                outDate: '',
                outCode: '',
                id: '',
                checkDate: '',
                checkStatus: '',
                checkUser: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
                createDate: '',
            };
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.sparePartOut.id == (null || '') ? "../sparepartout/save" : "../sparepartout/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.sparePartOut),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                    //更新备件清单-库存数量
                    Ajax.request({
                        url: "../sparepart/queryAll",
                        params: {partCode: vm.sparePartOut.partCode},
                        async: false,
                        successCallback: function (r) {
                            var sparePart = r.list[0];
                            sparePart.stockNum = sparePart.stockNum - vm.sparePartOut.outNum;
                            Ajax.request({
                                url: "../sparepart/update",
                                params: JSON.stringify(sparePart),
                                type: "POST",
                                contentType: "application/json",
                                successCallback: function (r) {
                                }
                            });
                        }
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            vm.getInfo(ids);
            confirm('确定要删除选中的记录？', function () {
                vm.sparePartOut.delFlag = 0;
                Ajax.request({
                    url: "../sparepartout/update",
                    params: JSON.stringify(vm.sparePartOut),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
            /*confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../sparepartout/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });*/
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../sparepartout/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.sparePartOut = r.sparePartOut;
                }
            });
        },
        getSparePart: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择备件',
                content: '../sys/sparepart.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSparePart();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        //     console.log(res)
                        vm.toolsPartOut.systemCode = res.erpCode, //系统编码
                            //   vm.toolsPartOut.name = res.name;//名称
                            vm.toolsPartOut.spec = res.specs;//规格
                        vm.toolsPartOut.partUnit = res.unit;//单位
                        vm.toolsPartOut.outStore = res.workshopName;//出库仓库
                        vm.toolsPartOut.outStoreId = res.workshop;//出库仓库
                        vm.toolsPartOut.cargoLocation = res.cargoLocation;//货位
                        vm.toolsPartOut.partId = res.belongTools;//所属工装

                        //console.log(vm.toolsPartOut.outCode)
                    }
                }
            })
        },
        reload: function (event) {
            vm.showList = true;
            let param = {};
            if(vm.q.outCode){
                param.outCode = vm.q.outCode;
            }
            if(vm.q.partName){
                param.partName = vm.q.partName;
            }
            if(vm.q.spec){
                param.spec = vm.q.spec;
            }
            if(vm.q.producor){
                param.producor = vm.q.producor;
            }
            if(vm.q.beginTime){
                param.beginTime = gmt2DateStr(vm.q.beginTime);
            }
            if(vm.q.endTime){
                param.endTime = gmt2DateStr(vm.q.endTime);
            }
            // 清空postData缓存数据
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: param,
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                outCode:'', // 出库编码
                partName: '', // 备件名称
                spec: '', // 备件规格
                producor:'', // 供应厂家
                beginTime: '', // 起始时间
                endTime:'' // 结束
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        //显示备件列表-物料编码
        getSparePart: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择备件',
                content: '../spare/sparePartSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSparePart();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.sparePart = res;
                        vm.sparePartOut.partCode = vm.sparePart.partCode;
                        vm.sparePartOut.name = vm.sparePart.name;
                        vm.sparePartOut.spec = vm.sparePart.spec;
                        vm.sparePartOut.partUnit = vm.sparePart.useUnit;
                    }
                }
            })
        },
        //显示设备列表-使用设备码
        getDeviceInfo: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择设备',
                content: '../sys/deviceInfoSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDeviceInfo();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.sparePartOut.useDevCode = res.devCode;
                        vm.sparePartOut.useDevName = res.devName;
                        vm.sparePartOut.useDevId = res.id;
                    }
                }
            })
        }
    }
});
